package com.example.demo7_data;

import androidx.appcompat.app.AppCompatActivity;

import android.content.ContentResolver;
import android.database.ContentObserver;
import android.net.Uri;
import android.os.Bundle;
import android.os.Handler;
import android.telecom.ConnectionService;
import android.util.Log;

public class MainActivity extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        Uri uri=Uri.parse("content://com.example.demo7/info");
        //注册内容观察者
        getContentResolver().registerContentObserver(uri,true,new MyObserver(new Handler()));
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        //取消内容
        getContentResolver().unregisterContentObserver(new MyObserver(new Handler()));
    }

    private class MyObserver extends ContentObserver
    {

        public MyObserver(Handler handler) {
            super(handler);
        }

        @Override
        public void onChange(boolean selfChange) {
            Log.d("监测数据变化","有人动了您的数据库数据！");
            super.onChange(selfChange);
        }
    }
}